package de.westranger.forex.trading.genetic.allel.fitness;

import java.text.ParseException;

import de.westranger.forex.trading.GlobalConfig;
import de.westranger.forex.trading.genetic.ContentResolver;
import de.westranger.forex.trading.genetic.allel.chromosom.CompositeChromosom;
import de.westranger.forex.trading.genetic.allel.operator.UnknownOperatorException;

public final class ComputeCompositeFitnesWorker implements Runnable {

	private final int startPos;
	private final int stopPos;
	private final CompositeChromosom[] population;
	private final ContentResolver conRes;
	
	public ComputeCompositeFitnesWorker(final CompositeChromosom[] population,final ContentResolver conRes,final int startPos,final int stopPos){
		this.population = population;
		this.startPos = startPos;
		this.stopPos = stopPos;
		this.conRes = conRes;
	}

	@Override
	public void run() {
		//final ComputeFitness ccf = new ComputeCompositeFitness(conRes);
		//final ComputeFitness ccf = new MoneyManagedFitness(conRes,GlobalConfig.MONEY,GlobalConfig.MONEY_INVEST,GlobalConfig.LEVERAGE);
		final ComputeFitness ccf = new MoneyManagedFitness(conRes,GlobalConfig.MONEY,GlobalConfig.MONEY_INVEST,GlobalConfig.LEVERAGE);
		CompositeFitness cf;
		try {
			for(int i=startPos;i<stopPos;i++){
				 cf = ccf.computeFitness(population[i]);
				population[i].setFitness(cf.calcRatio());
			}
		} catch (UnknownOperatorException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}


}
